﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using System;
	using System.Collections.Generic;
	using Falcon.Core.Authentication;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	public class UserMessage : EntityBase<UserMessage>
	{
		// private fields
		private User _sender;
		private User _receiver;
		private UserMessageFolder _folder;

		/// <summary>
		/// Initializes a new instance of the <see cref="UserMessage"/> class.
		/// </summary>
		public UserMessage()
		{
			this.Identity = User.Identity;

			if (this.Identity != null && this.ReceiverId == 0 && this.SenderId == 0)
			{
				this.ReceiverId = this.Identity.Id;
			}

			DeterminePermission(Roles.UsersMessages);
		}

		/// <summary>
		/// Gets or sets the parent id.
		/// </summary>
		/// <value>The parent id.</value>
		public virtual int ParentId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the sender id.
		/// </summary>
		/// <value>The sender id.</value>
		public virtual int SenderId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the receiver id.
		/// </summary>
		/// <value>The receiver id.</value>
		public virtual int ReceiverId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the folder id.
		/// </summary>
		/// <value>The folder id.</value>
		public virtual int FolderId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public virtual string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the slug.
		/// </summary>
		/// <value>The slug.</value>
		public virtual string Slug
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public virtual string Body
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body parsed.
		/// </summary>
		/// <value>The body parsed.</value>
		public virtual string BodyParsed
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether the sender is active.
		/// </summary>
		/// <value><c>true</c> if the sender is active; otherwise, <c>false</c>.</value>
		public virtual bool SenderActive
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether the receiver is active.
		/// </summary>
		/// <value><c>true</c> if the receiver is active; otherwise, <c>false</c>.</value>
		public virtual bool ReceiverActive
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether the receiver has read.
		/// </summary>
		/// <value><c>true</c> if the receiver has read; otherwise, <c>false</c>.</value>
		public virtual bool ReceiverRead
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether the receiver has replied.
		/// </summary>
		/// <value><c>true</c> if the receiver has replied; otherwise, <c>false</c>.</value>
		public virtual bool ReceiverReplied
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether the receiver has forwarded.
		/// </summary>
		/// <value><c>true</c> if the receiver has forwarded; otherwise, <c>false</c>.</value>
		public virtual bool ReceiverForwarded
		{
			get;
			set;
		}

		/// <summary>
		/// Gets a value indicating whether this instance is receiver.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is receiver; otherwise, <c>false</c>.
		/// </value>
		public virtual bool IsReceiver
		{
			get
			{
				return this.Identity != null && this.ReceiverId.Equals(this.Identity.Id);
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance is sender.
		/// </summary>
		/// <value><c>true</c> if this instance is sender; otherwise, <c>false</c>.</value>
		public virtual bool IsSender
		{
			get
			{
				return this.Identity != null && this.SenderId.Equals(this.Identity.Id);
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance is owner.
		/// </summary>
		/// <value><c>true</c> if this instance is owner; otherwise, <c>false</c>.</value>
		public virtual bool IsOwner
		{
			get
			{
				return (this.IsReceiver || this.IsSender);
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can view.
		/// </summary>
		/// <value><c>true</c> if this instance can view; otherwise, <c>false</c>.</value>
		public override bool CanView
		{
			get
			{
				return base.CanView && this.IsOwner;
			}
		}

		/// <summary>
		/// Gets or sets the read.
		/// </summary>
		/// <value>The read.</value>
		public virtual DateTime? Read
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the date created.
		/// </summary>
		/// <value>The created.</value>
		public virtual DateTime Created
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the date updated.
		/// </summary>
		/// <value>The updated.</value>
		public virtual DateTime Updated
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the sender.
		/// </summary>
		/// <value>The sender.</value>
		public virtual User Sender
		{
			get
			{
				return _sender;
			}
			set
			{
				_sender = value;

				if (_sender != null)
				{
					this.SenderId = _sender.Id;
				}
			}
		}

		/// <summary>
		/// Gets or sets the receiver.
		/// </summary>
		/// <value>The receiver.</value>
		public virtual User Receiver
		{
			get
			{
				return _receiver;
			}
			set
			{
				_receiver = value;

				if (_receiver != null)
				{
					this.ReceiverId = _receiver.Id;
				}
			}
		}

		/// <summary>
		/// Gets or sets the folder.
		/// </summary>
		/// <value>The folder.</value>
		public virtual UserMessageFolder Folder
		{
			get
			{
				return _folder;
			}
			set
			{
				_folder = value;

				if (_folder != null)
				{
					this.FolderId = _folder.Id;
				}
			}
		}

		/// <summary>
		/// Gets or sets the identity.
		/// </summary>
		/// <value>The identity.</value>
		public virtual WebUserIdentity Identity
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the attachments.
		/// </summary>
		/// <value>The attachments.</value>
		public virtual IList<Attachment> Attachments
		{
			get;
			set;
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserMessage> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			rules.Add(x => x.Body)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Message));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}